const fs = require('fs');
const sizeOf = require('image-size');

const isOpenDefault = true;

const config = {
    'other': {
        isSaveWhDic: false, // 存储宽高比，用于初步确定模型训练时anchor的大小（最后resnext里还是使用了原始的anchor 16，32，64，128，256）
        isSaveHandDic: false, // 存储位置大小字典，用于在html中查看数据信息（用于手动校验检查，最后输出时，此项需关闭）
        isDetect: true, // 是否是由检测直接后处理生成匹配结果
        isRemoveMatch: true, // 是否删除原有的match，重构全部匹配来删选
    },
    'v0.0.1': { // 删除【type不同】的匹配
        'isOpen': isOpenDefault,
    },
    'v0.0.2': { // 增加【match中不存在的sign】的匹配
        'isOpen': isOpenDefault,
    },
    'v0.1': { // 删除【绝对距离过远】的匹配
        'isOpen': isOpenDefault,
        'dx': 700,
        'dy': 400
    },
    'v0.3': { // 删除【相对距离过远】并且【图标大小差距不大】的匹配
        'isOpen': isOpenDefault,
        'pdx': 0.32,
        'pdy': 0.32,
        'dw': 15,
        'dh': 15
    },
    'v0.4': { // 删除【相对位置】与目标图不同的匹配，比如一根柱子上【左侧的摄像头】匹配成了【右侧的摄像头】
        'isOpen': isOpenDefault
    },
    'v1.1': { // 删除或改变【在时间序列中出现频率较小】的类型
        'isOpen': isOpenDefault,
        'deleteOrChange': 'change',
        'smallNum': 2, // 小于等于这个数字的type类 有移除可能
        'biggerThanSmall': 1.5, // 小于最大值这个系数的type类 有移除可能
    },
    'v1.2': { // 删除【与其他检测iOU过于接近的图】
        'isOpen': isOpenDefault,
        'iOULimit': .8,
        'isShowLog': false, // 是否展示iOU大于限定值的
    }
}
const resultPath = './fu.detect.resnext'; // result输入目录
// const resultPath = './handDemo';
const resultOutPath = './out'; // 后处理输出目录
const picPath = '' // ！！！result推导的原始图片路径，用于获取图片宽高来计算相对位置，如果不设置，就默认图片为1280*720，事实上测试集几万张图片（41125张）中有几千张（3401张）是1920*1080的，有几十张（84张）是1920*720的
// let handDic = JSON.parse(fs.readFileSync('handDic.0.49332.json').toString());  // 用于手动查看图片数据的json文件

if (!fs.existsSync(resultOutPath)) {
    fs.mkdirSync(resultOutPath)
}

if (!picPath) {
    console.log('WARNING: 建议配置picPath来获取准确的相对位置 来进行后处理')
}

let files = fs.readdirSync(resultPath);

let a = 0;
let xf = 0;
let idxWrongC = 0;
let no301Num = 0;
let is301Num = 0;
let typeDic = {};
let notMatchedCount = 0
let singsCount = 0
let matchsCount = 0;
let ANumCount = 0
let BNumCount = 0
let AxBNumCount = 0;
let dic = {}

let nmsC = 0;
let nmsAfter = 0;

let whDic = {};

for (let i = 0; i < files.length; i++) {
    const fileName = files[i];
    if (i % 200 == 0) {
        console.log(`进度：${((i / files.length)*100).toFixed(2)}%`);
    }
    const json = JSON.parse(fs.readFileSync(`${resultPath}/${fileName}`).toString());
    // 处理detect
    if (config['other']['isDetect'] && resultOutPath !== resultPath) {
        json.match = [];
    }
    // console.log(json.match.length);
    a += json.match.length;
    // 记录图片找到的点汇总到一个图中
    var signs = json.signs;
    singsCount+= signs.length;

    let picGroupDic = {}
    let groupCountDic = {}
    if (config['other']['isDetect']) {
        let group = json.group;
        for (let j = 0; j < group[0].pic_list.length; j++) {
            let pic_id = group[0].pic_list[j];
            picGroupDic[pic_id] = group[0].seq_id;
        } 
        for (let j = 0; j < group[1].pic_list.length; j++) {
            let pic_id = group[1].pic_list[j];
            picGroupDic[pic_id] = group[1].seq_id;
        } 
        groupCountDic['A'] = groupCountDic['A'] || 0;
        groupCountDic['B'] = groupCountDic['B'] || 0;
        for (let j = 0; j < signs.length; j++) {
            let sign = signs[j];
            let group = picGroupDic[sign.pic_id];
            sign.sign_id = `sign_${group}_${++groupCountDic[group]}`;
        }
    }

    if (config['v1.1'].isOpen) {
        // 判断同批非301物体
        // v0.1.1 2020-08-19 13:16
        let v11no301SideSignDic = {}
        let bigSignNum = 0;
        let bigSignType = ''
        for (let j = 0; j < signs.length; j++) {
            let sign = signs[j];
            if (sign.type !== '301') {
                let {type,sign_id} = sign;
                let side = 'mix'//sign_id[5];
                v11no301SideSignDic[side] = v11no301SideSignDic[side] || {};
                v11no301SideSignDic[side][type] = v11no301SideSignDic[side][type] || 0;
                v11no301SideSignDic[side][type]++
                if (v11no301SideSignDic[side][type] > bigSignNum) {
                    bigSignNum = v11no301SideSignDic[side][type];
                    bigSignType = type;
                }
            }
        }
        // console.log(fileName, v11no301SideSignDic)
        let no301MethodNewSigns = []
        for (let j = 0; j < signs.length; j++) {
            let sign = signs[j];
            let isNeedPush = true;
            if (sign.type !== '301') {
                let {type,sign_id} = sign;
                let side = 'mix'//sign_id[5];
                let typeNum = v11no301SideSignDic[side][type]
                // 比如 错误的1个，总共正确的有2个，那把错误的改成正确的
                // console.log(typeNum, bigSignNum)
                if (typeNum <= config['v1.1']['smallNum'] && typeNum* config['v1.1']['biggerThanSmall'] <= bigSignNum ) {
                    // 更改不符合大流的sign
                    if (config['v1.1']['deleteOrChange'] == 'change') {
                        sign.type = bigSignType;
                    }
                    isNeedPush = false;
                }
            }
            if (isNeedPush) {
                no301MethodNewSigns.push(sign)
            }
        }
        // 删除不符合大流的sign
        if (config['v1.1']['deleteOrChange'] == 'delete') {
            json.signs = no301MethodNewSigns
            signs = json.signs;
        }
    }
    
    if (config['v1.2'].isOpen) {
        // 居然还有重复的，再加一个nms
        // v0.1.2 2020-08-19 13:37
        let picSignArrDic = {}
        let newSigns = [];
        for (let j = 0; j < signs.length; j++) {
            let sign = signs[j];
            let {pic_id} = sign;
            picSignArrDic[pic_id] = picSignArrDic[pic_id] || [];
            let picSignArr = picSignArrDic[pic_id]
            let isExistNear = false;
            for (let k = 0; k < picSignArr.length; k++) {
                const _sign = picSignArr[k];
                let iOU = getIOU(sign, _sign);
                if (iOU > config['v1.2']['iOULimit']) {
                    if (config['v1.2'].isShowLog) {
                        console.log(_sign,fileName, sign)
                    }
                    isExistNear = true;
                }
            }
            if (!isExistNear) {
                picSignArrDic[pic_id].push(sign);
                newSigns.push(sign);
            }
        }
        // nmsC++
        nmsC += signs.length
        if (signs.length - newSigns.length >= 0) {
            // console.log(signs.length, newSigns.length , signs.length - newSigns.length)
            // nmsAfter++
            nmsAfter += newSigns.length
            // console.log('nms delete percent:', (nmsC-nmsAfter)/nmsC)
        }
        signs = newSigns;
        json.signs = signs;


        function mMax(n1, n2) {
            if (n1 > n2 ) {
                return n1;
            } else {
                return n2;
            }
        }
        function mMin(n1, n2) {
            if (n1 < n2 ) {
                return n1;
            } else {
                return n2;
            }
        }
        // 计算两个标记之间的重合比例
        function getIOU(sign1, sign2) {
            let mixAreaX = mMax(sign1['x'], sign2['x'])
            let mixAreaY = mMax(sign1['y'], sign2['y'])
            let mixAreaX2 = mMin(sign1['x'] + sign1['w'], sign2['x'] + sign2['w'])
            let mixAreaY2 = mMin(sign1['y'] + sign1['h'], sign2['y'] + sign2['h'])
            let sign1Area = sign1['w'] * sign1['h']
            let sign2Area = sign2['w'] * sign2['h']
            let mixAreaW = mixAreaX2 - mixAreaX
            let mixAreaH = mixAreaY2 - mixAreaY
            // console.log(mixAreaW, mixAreaH)
            if (mixAreaW > 0 && mixAreaH > 0) {
                mixArea = mixAreaW * mixAreaH
                return mixArea / (sign1Area + sign2Area - mixArea)
            } else {
                return 0
            }
        }
    }

    // 补充match
    // v0.1.0 2020-08-15 02:51
    let ANum = 0;
    let BNum = 0;
    let ASigns = [];
    let BSigns = [];
    let newMatch = [];
    let AxBNum = 0;
    let has301Signs = [];
    let no301Signs = [];
    for (let j = 0; j < signs.length; j++) {
        const sign = signs[j];
        if (sign.type == '301') {
            has301Signs.push(sign);
        }
        if (sign.type !== '301') {
            no301Signs.push(sign);
        }
        delete sign.score;
        sign.w = parseInt(sign.w)
        sign.h = parseInt(sign.h)
        sign.x = parseInt(sign.x)
        sign.y = parseInt(sign.y)
        sign.sign_id = sign.sign_id || '';
        if (sign.sign_id.indexOf('A') > -1){
            ANum++;
            ASigns.push(sign.sign_id)
        }
        if (sign.sign_id.indexOf('B') > -1){
            BNum++;
            BSigns.push(sign.sign_id)
        }
        if (config['other']['isSaveWhDic']) {
            let key = `${parseInt(sign.w)}-${parseInt(sign.h)}`
            whDic[key] = whDic[key] || 0;
            whDic[key]++;
        }
    }
    // 改train tag
    // json.signs = has301Signs;
    // fs.writeFileSync(`trainHas301Signs/${fileName}`, JSON.stringify(json)); // 保存301sign
    // json.signs = no301Signs;
    // fs.writeFileSync(`trainNo301Signs/${fileName}`, JSON.stringify(json)); // 保存非301sign
    // continue
    for (let _i = 0; _i < ASigns.length; _i++) {
        for (let _j = 0; _j < BSigns.length; _j++) {
            newMatch.push({
                    "sign_id": ASigns[_i], 
                    "match_sign_id": BSigns[_j]
            })
        }
    }
    if (resultPath !== resultOutPath) {
        if (config['other']['isRemoveMatch']) {
            json.match = newMatch;
        }
    }
    AxBNum = ANum * BNum;
    ANumCount+=ANum;
    BNumCount+=BNum;
    AxBNumCount+=AxBNum
    matchsCount += json.match.length;
    // console.log('上升空间', AxBNum, json.match.length, json.match.length / AxBNum, matchsCount/AxBNumCount)
    // console.log(matchsCount/AxBNumCount)



    var picMatchDix301Dic = {}
    for (let k = 0; k < signs.length; k++) {
        const sign = signs[k];
        picMatchDix301Dic[sign.pic_id] = picMatchDix301Dic[sign.pic_id] || [];
        if (sign.type == '301') {
            picMatchDix301Dic[sign.pic_id].push(sign);
        }
        // if (sign.type == '102') {
        //     console.log(sign, fileName)
        // }
    }
    
    let tarMatch = []
    for (let j = 0; j < json.match.length; j++) {
        const _match = json.match[j];
        // console.log(_match, fileName, json.signs)
        let a = findSrc(_match.sign_id, json.signs);
        let b = findSrc(_match.match_sign_id, json.signs);
        if (picPath && !fs.existsSync(`${picPath}/${a.pic_id}.jpg`)) {
            console.log(`no ${picPath}/${a.pic_id}.jpg`)
            continue;
        }
        var paw, pbh = 1280;
        var pah, pbh = 720;
        // 直接筛选掉type不同的
        if (a && b) {
            // 查询图片宽高
            if (picPath) {
                var {width:paw, height: pah} = sizeOf(fs.readFileSync(`${picPath}/${a.pic_id}.jpg`))
                var {width:pbw, height: pbh} = sizeOf(fs.readFileSync(`${picPath}/${b.pic_id}.jpg`))
            }
            const dax = a.x / paw;
            const dbx = b.x / pbw;
            const day = a.y / pah;
            const dby = b.y / pbh;

            let aArr = picMatchDix301Dic[a.pic_id]
            let bArr = picMatchDix301Dic[b.pic_id]
            let aWIdx = findPicWIdx(a.sign_id, aArr)
            let bWIdx = findPicWIdx(b.sign_id, bArr)
            let aHIdx = findPicHIdx(a.sign_id, aArr)
            let bHIdx = findPicHIdx(b.sign_id, bArr)
            // 
            if (false) {

            }
            else if (config['v0.0.1'].isOpen && a.type != b.type) { // 去除类型不同的
                xf++;
            }
            // // // v0.01
            // else if (Math.abs(a.x - b.x) > 700 || Math.abs(a.y - b.y) > 400) {
            //     xf++;
            // }
            else if (config['v0.1'].isOpen && Math.abs(a.x - b.x) > config['v0.1'].dx || Math.abs(a.y - b.y) > config['v0.1'].dy) {
                xf++;
            }
            // // v0.0.2 2020-08-13 00:22 直接比，但图片两类大小
            // // else if ((Math.abs(a.x - b.x) > 456 || Math.abs(a.y - b.y) > 256) && ((Math.abs(a.w- b.w) < 15) && (Math.abs(a.h- b.h) < 15)) && a.type == '301') {
            // //     xf++;
            // // }
            // // v0.0.3 2020-08-13 02:54 按比例
            // else if ((Math.abs(dax - dbx)>0.36 || Math.abs(day - dby) > 0.36) && ((Math.abs(a.w- b.w) < 15) && (Math.abs(a.h- b.h) < 15)) && a.type == '301') {
            //     xf++
            // }
            else if (config['v0.3'].isOpen && (Math.abs(dax - dbx)>config['v0.3'].pdx || Math.abs(day - dby) > config['v0.3'].pdy) && ((Math.abs(a.w- b.w) < config['v0.3'].dw) && (Math.abs(a.h- b.h) < config['v0.3'].dh)) && a.type == '301') {
                xf++
            }
            // // v0.0.4 2020-08-14 00:22 按图顺序找一遍，个数一样，左右顺序不对的，直接去掉
            else if (config['v0.4'].isOpen && aArr.length == bArr.length && aWIdx != bWIdx && aHIdx != bHIdx) {
                // if (a.pic_id == '19593179120900000631' && b.pic_id == '18558863091900001677') {
                    // console.log('aArr.length == bArr.length && aWIdx != bWIdx', aArr.length, bArr.length,  aWIdx, bWIdx)
                    // console.log('fileName aPic bPic', fileName, a.pic_id, b.pic_id)
                // }
                idxWrongC++;
                xf++
            }
            else {
                tarMatch.push(_match);
            }
        } else {
            tarMatch.push(_match);
        }
        // 存储用于手动鉴别
        if (config['other']['isSaveHandDic']) {
            _match.a = a;
            _match.b = b;
        }
        dic[`${fileName}-${j}`] = _match;
    }
    json.match = tarMatch;
    fs.writeFileSync(`${resultOutPath}/${fileName}`, JSON.stringify(json)); // 去掉type不对的
    
    if (config['v0.0.2'].isOpen) {
    // 判断是否在match中
        for (let j = 0; j < signs.length; j++) {
            const sign = signs[j];
            if (sign.type != '301') no301Num++;
            if (sign.type == '301') is301Num++;
            typeDic[sign.type] = typeDic[sign.type] || 0;
            if (sign.type=='205') {
                // console.log(sign, fileName);
            }
            typeDic[sign.type]++
            let match = json.match;
            let isMatched = false;
            for (let z = 0; z < match.length; z++) {
                const element = match[z];
                if (element.sign_id == sign.sign_id || element.match_sign_id == sign.sign_id) {
                    isMatched = true
                }
            }
            if (!isMatched) {
                notMatchedCount++
                json.match.push({
                    sign_id: sign.sign_id,
                    match_sign_id: ""
                })
            }
        }
        fs.writeFileSync(`${resultOutPath}/${fileName}`, JSON.stringify(json)); // 保存漏掉的单个
    }
}
if (config['other']['isSaveWhDic']) {
    fs.writeFileSync(`whDic.json`, JSON.stringify(whDic)); // 统计目标宽高占比

    // console.log(whDic)
    let c = 0;
    let b = 0
    let wDic = {}
    let hDic = {}
    let whArr = []
    for (const wh in whDic) {
        if (whDic.hasOwnProperty(wh)) {
            const count = whDic[wh];
            let w = Number(wh.split('-')[0])
            let h = Number(wh.split('-')[1])
            // if (Math.abs(6-w) < 0.22*w && Math.abs(8-h) < 0.22*h) {
            //     continue
            // }
            // if (Math.abs(9-w) < 0.22*w && Math.abs(12-h) < 0.22*h) {
            //     continue
            // }
            // if (Math.abs(4-w) < 0.22*w && Math.abs(6-h) < 0.22*h) {
            //     continue
            // }
            // if (Math.abs(5-w) < 0.22*w && Math.abs(5-h) < 0.22*h) {
            //     continue
            // }
            // if (Math.abs(8-w) < 0.22*w && Math.abs(8-h) < 0.22*h) {
            //     continue
            // }
            // if (Math.abs(12-w) < 0.22*w && Math.abs(12-h) < 0.22*h) {
            //     continue
            // }
            // if (Math.abs(13-w) < 0.22*w && Math.abs(15-h) < 0.22*h) {
            //     continue
            // }
            // if (Math.abs(18-w) < 0.22*w && Math.abs(18-h) < 0.22*h) {
            //     continue
            // }
            // if (Math.abs(30-w) < 0.22*w && Math.abs(30-h) < 0.22*h) {
            //     continue
            // }
            // if (Math.abs(30-w) < 0.22*w && Math.abs(30-h) < 0.22*h) {
            //     continue
            // }
            if (Math.abs(25-w) < 0.33*w && Math.abs(25-h) < 0.33*h) {
                continue
            }
            if (Math.abs(13-w) < 0.33*w && Math.abs(13-h) < 0.33*h) {
                continue
            }
            if (Math.abs(40-w) < 0.33*w && Math.abs(40-h) < 0.33*h) {
                continue
            }
            c+= count;
            b += h/w*count;
            wDic[w] = wDic[w] ||0;
            wDic[w] += count;
            hDic[h] = hDic[h] ||0;
            hDic[h] += count;
            whArr.push({
                w,
                h,
                count
            })
            if (count > 100) {
                // console.log(wh, count)
            }
        }
    }
    whArr.sort((a,b)=>{
        return b.count-a.count
    })
    // console.log(whArr)
    // console.log(wDic, hDic)
    console.log(c, b/c);
}

console.log('已完成', '100%')
// xf:被删除的match量 a:被通过的量 notMatchedCount:未在match中的sign量
console.log(xf, a, notMatchedCount, `(idxWrongC:${idxWrongC})`, `(no301Num:${no301Num};is301Num:${is301Num})`, `(singsCount:${singsCount})`)
console.log(typeDic)

function findSrc(key, signs) {
    for (let i = 0; i < signs.length; i++) {
        const sign = signs[i];
        if (sign.sign_id == key) {
            return sign;
        }
    }
}
// 获取sign在图片中从左到右的坐标位置
function findPicWIdx(sign_id, arr) {
    arr.sort((a,b)=>{
        return a.x - b.x
    })
    for (let i = 0; i < arr.length; i++) {
        const sign = arr[i];
        if (sign.sign_id == sign_id) {
            return i
        }
    }
}
// 获取sign在图片中从上到下的坐标位置
function findPicHIdx(sign_id, arr) {
    arr.sort((a,b)=>{
        return a.y - b.y
    })
    for (let i = 0; i < arr.length; i++) {
        const sign = arr[i];
        if (sign.sign_id == sign_id) {
            return i
        }
    }
}
if (config['other']['isSaveHandDic']) {
    fs.writeFileSync('handDic.js', `const handDic = ${JSON.stringify(dic)}`) // 保存手动鉴别   
}


// 自定义删除出错的match
// let keyArr = Object.keys(handDic);

// // let tarHandIdxDic = {
// //     "0":0,"1":1,"2":0,"3":1,"4":0,"5":0,"6":0,"7":0,"8":0,"9":1,"10":0,"11":0,"12":1,"13":0,"14":1,"15":1,"16":1,"17":1,"18":0,"19":1,"20":1,"21":0,"22":1,"23":0,"24":1,"25":1,"26":1,"27":1,"28":0,"29":1,"30":0,"31":0,"32":1,"33":0,"34":1,"35":1,"36":1,"37":1,"38":1,"39":0,"40":1,"41":0,"42":1,"43":0,"44":0,"45":1,"46":0,"47":0,"48":0,"49":0,"50":0,"51":0,"52":0,"53":0,"54":0,"55":0,"56":0,"57":1,"58":1,"59":0,"60":0,"61":0,"62":0,"63":0,"64":1,"65":0,"67":0,"68":0,"69":0,"70":1,"71":1,"73":0,"74":0,"75":1,"76":0,"77":0,"78":0,"79":0,"80":0,"81":1,"83":0,"84":0,"85":0,"86":0,"87":1,"88":0,"89":0,"90":0,"91":0,"92":0,"93":0,"94":0,"95":0,"96":0,"97":0,"98":0,"99":0,"100":0,"101":1,"102":1,"103":1,"104":1,"105":1,"106":1,"107":0,"108":0,"109":0,"110":0,"111":0,"112":0,"113":1,"114":1,"115":1,"116":0,"117":0,"118":0,"119":0,"120":1,"121":1,"122":1,"123":1,"124":1,"125":0,"126":0,"127":0,"128":0,"129":1,"130":1,"131":1,"132":0,"133":0,"134":0,"135":0,"136":0,"137":0,"138":0,"139":0,"140":0,"141":1,"142":0,"143":0,"144":0,"145":0,"146":0,"147":0,"148":0,"149":0,"150":0,"151":0,"152":0,"153":0,"154":0,"155":1,"156":1,"157":1,"158":0,"159":0,"160":0,"161":0,"162":1,"163":1,"164":0,"165":1,"166":0,"167":0,"168":0,"169":0,"170":0,"171":0,"175":1,"178":0,"179":0,"180":0,"181":0,"182":0,"183":0,"184":0,"185":0,"186":0,"187":0,"188":0,"189":0,"190":0,"191":0,"192":0,"193":0,"194":0,"195":0,"196":0,"197":0,"198":0,"199":0,"200":0,"201":0,"202":0,"203":0,"204":0,"205":0,"206":1,"207":1,"208":1,"209":1,"210":1,"211":1,"212":1,"213":1,"214":1,"215":1,"216":1,"217":1,"218":1,"219":1,"220":1,"221":1,"222":1,"223":1,"224":1,"225":1,"226":1,"227":1,"228":1,"229":0,"230":0,"231":0,"233":1,"234":1,"235":1,"236":1,"237":0,"238":0,"241":1,"242":1,"243":1,"244":1,"245":0,"248":0,"249":1,"250":1,"251":1,"252":1,"253":1,"254":1,"255":1,"256":1,"257":1,"258":1,"259":1,"260":1,"261":1,"262":1,"264":1,"265":1,"266":1,"267":1,"268":1,"269":1,"270":0,"271":0,"272":0,"273":0,"274":0,"275":0,"276":0,"277":0,"278":0,"279":0,"280":0,"281":0,"282":0,
// // }
// //     // "2001":1,"2002":0,"2003":0,"2004":1,"2005":1,"2006":1,"2007":0,"2008":0,"2009":0,"2010":0,"2011":1,"2012":0,"2013":0,"2014":0,"2015":0,"2016":0,"2017":0,"2018":0,"2019":0,"2020":0,"2021":0,"2022":0,"2023":1,"2024":0,"2025":1,"2026":0,"2027":0,"2028":0,"2029":0,"2030":0,"2031":0,"2032":0,"2033":1,"2034":1,"2035":1,"2036":0,"2037":1,"2038":0,"2039":0,"2040":0,"2041":0,"2042":0,"2043":1};
// let tarHandIdxDic = {
//     "10000":1,"10001":1,"10002":1,"10003":1,"10004":0,"10006":0,"10009":0,"10011":0,"10017":0,"10019":0,"10020":0,"10022":0,"10023":0,"10024":0,"10026":0,"10027":0,"10028":0,"10029":0,"10030":0,"10031":0,"10032":0,"10033":0,"10034":0,"10035":0,"10036":0,"10039":1,"10040":1,"10041":0,"10042":0,"10043":0,"10044":0,"10069":1,"10070":1,"10071":1,"10072":1,"10073":1,"10074":1,"10075":1,"10076":1,"10077":1,"10078":1,"10079":1,"10080":1,"10081":1,"10082":1,"10083":1,"10084":0,"10085":1,"10086":1,"10087":1,"10088":1,"10089":1,"10090":1,"10091":1,"10092":0,"10093":1,"10094":1,"10095":1,"10096":1,"10097":1,"10098":0,"10099":0,"10101":1,"10102":1,"10103":1,"10104":1,"10105":1,"10106":1,"10107":1,"10108":0,"10109":0,"10110":0,"10111":1,"10112":1,"10113":0,"10114":1,"10115":0,"10117":1,"10118":1,"10119":1,"10120":1,"10121":1,"10122":1,"10123":1,"10124":1,"10125":1,"10126":1,"10127":1,"10128":1,"10129":1,"10130":1,"10131":0,"10132":1,"10133":0,"10134":1,"10135":1,"10136":0,"10137":1,"10138":0,"10139":0,"10140":0,"10141":1,"10142":1,"10143":0,"10144":0,"10145":1,"10146":1,"10147":1,"10148":1,"10149":0,"10150":1,"10151":0,"10152":1,"10153":0,"10154":0,"10155":0,"10156":1,"10157":0,"10158":1,"10159":1,"10160":0,"10161":0,"10162":1,"10163":1,"10164":0,"10165":0,"10166":1,"10167":0,"10168":0,"10169":0,"10170":0,"10171":1,"10172":0,"10175":0,"10177":0,"10178":0,"10179":0,"10180":0,"10181":0,"10182":1,"10183":1,"10184":0,"10185":1,"10186":1,"10187":0,"10188":0,"10189":0,"10190":0,"10192":0,"10193":0,"10195":1,"10196":0,"10197":0,"10198":0,"10199":1,"10200":1,"10201":0,"10202":1,"10203":1,"10204":1,"10205":0,"10206":0,"10207":1,"10208":0,"10212":1,"10213":0,"10214":0,"10217":1,"10218":1,"10219":0,"10220":0,"10221":1,"10222":1,"10223":0,"10224":1,"10225":0,"10228":1,"10229":1,"10230":0,"10231":1,"10232":0,"10233":1,"10234":0,"10236":0,"10239":0,"10240":0,"10241":0,"10242":0,"10243":0,"10245":1,"10246":0,"10247":1,"10248":1,"10249":0,"10250":0,"10251":0,"10252":1,"10253":1,"10254":0,"10255":0,"10256":1,"10257":0,"10258":1,"10259":0,"10261":0,"10262":0,"10263":1,"10264":0,"10266":1,"10267":1,"10268":1,"10269":1,"10270":1,"10271":1,"10272":1,"10273":0,"10275":1,"10276":1,"10277":1,"10278":1,"10279":1,"10280":1,"10281":1,"10282":1,"10283":1,"10284":1,"10285":1,"10286":1,"10287":1,"10288":1,"10289":1,"10290":1,"10291":1,"10292":1,"10293":1,"10294":1,"10295":1,"10296":1,"10297":1,"10298":1,"10299":1,"10300":1,"10301":1,"10302":1,"10303":1,"10304":1,"10305":0,"10306":0,"10307":0,"10308":1,"10309":1,"10310":1,"10311":1,"10312":1,"10313":1,"10314":1,"10315":1,"10316":1,"10317":1,"10318":1,"10319":1,"10320":1,"10321":1,"10322":1,"10323":0,"10324":0,"10325":0,"10326":0,"10327":0,"10328":0,"10329":0,"10330":0,"10331":0,"10332":0,"10333":0,"10334":0,"10335":0,"10336":0,"10337":0,"10338":0,"10339":0,"10340":1,"10341":0,"10342":1,"10343":0,"10344":1,"10345":0,"10346":1,"10347":0,"10348":1,"10349":0,"10350":1,"10351":0,"10352":1,"10353":0,"10354":1,"10355":0,"10356":1,"10357":0,"10358":1,"10359":0,"10360":0,"10361":1,"10362":1,"10363":0,"10364":0,"10365":1,"10366":1,"10387":1,"10389":1,"10390":1,"10391":1,"10392":0,"10393":0,"10394":1,"10395":1,"10396":1,"10397":1,"10398":1,"10399":0,"10410":0,"10411":0,"10417":1,"10418":1,"10419":1,"10420":1,"10421":1,"10422":0,"10423":0,"10424":0,"10425":0,"10426":0,"10427":0,"10428":1,"10429":1,"10430":1,"10431":1,"10432":1,"10433":1,"10434":1,"10435":1,"10436":1,"10437":1,"10438":1,"10439":1,"10441":1,"10442":1,"10443":1,"10444":1,"10445":1,"10446":1,"10447":1,"10448":1,"10449":1,"10450":1,"10451":1,"10452":1,"10453":1,"10454":1,"10455":1,"10456":0,"10459":0,"10460":0,"10461":1,"10462":0,"10463":1,"10464":1,"10465":0,"10466":0,"10467":1,"10468":1,"10469":1,"10470":1,"10471":0,"10472":0,"10473":0,"10474":0,"10475":1,"10476":1,"10477":1,"10478":1,"10479":0,"10480":0,"10481":1,"10482":1,"10483":0,"10484":0,"10485":1,"10486":1,"10487":1,"10488":1,"10489":0,"10490":0,"10491":0,"10492":1,"10493":1,"10494":1,"10495":1,"10496":1,"10497":1,"10498":1,"10499":0,"10500":0,"10501":0,"10502":0,"10503":0,"10504":1,"10505":0,"10506":0,"10507":0,"10508":1,"10509":1,"10510":1,"10511":0,"10512":1,"10513":0,"10514":1,"10515":1,"10516":0,"10517":0,"10518":0,"10519":0,"10520":1,"10521":1,"10522":1,"10523":1,"10524":0,"10525":0,"10526":1,"10527":1,"10528":1,"10529":0,"10530":1,"10531":0,"10532":1,"10533":0,"10534":0,"10535":0,"10536":1,"10537":0,"10538":0,"10539":0,"10540":0,"10541":0,"10542":1,"10543":1,"10544":1,"10545":0,"10546":1,"10547":1,"10548":1,"10549":1,"10550":1,"10551":1,"10552":0,"10553":1,"10554":1,"10555":1,"10556":0,"10557":1,"10558":0,"10559":0,"10560":0,"10561":0,"10562":0,"10563":0,"10564":1,"10565":1,"10566":1,"10567":0,"10568":1,"10569":1,"10570":1,"10572":1,"10573":1,"10574":1,"10575":1,"10576":1,"10577":1,"10578":1,"10579":0,"10580":1,"10581":1,"10582":1,"10583":0,"10584":1,"10585":1,"10586":1,"10587":0,"10590":0,"10591":0,"10592":0,"10593":1,"10594":1,"10596":1,"10597":1,"10598":0,"10599":0,"10600":0,"10601":1,"10602":0,"10603":0,"10604":1,"10605":0,"10606":0,"10607":0,"10608":0,"10609":0,"10610":0,"10611":0,"10612":0,"10613":1,"10614":1,"10615":0,"10616":1,"10617":1,"10618":1,"10619":0,"10620":0,"10621":1,"10622":1,"10623":1,"10624":1,"10625":1,"10626":1,"10627":1,"10628":0,"10629":1,"10630":1,"10631":1,"10632":1,"10633":1,"10634":1,"10635":1,"10636":1,"10637":1,"10638":1,"10639":1,"10640":1,"10641":1,"10642":1,"10643":1,"10644":1,"10645":0,"10646":1,"10647":1,"10648":1,"10649":1,"10650":1,"10651":0,"10652":1,"10653":1,"10654":0,"10655":1,"10656":1,"10657":1,"10658":1,"10659":0,"10660":1,"10661":1,"10662":1,"10663":1,"10664":1,"10665":0,"10666":1,"10667":1,"10668":1,"10669":1,"10670":1,"10671":1,"10672":1,"10673":0,"10674":1,"10675":1,"10676":1,"10677":1,"10678":1,"10679":1,"10680":1,"10681":1,"10682":1,"10683":1,"10684":0,"10685":0,"10686":0,"10687":0,"10688":0,"10689":0,"10690":0,"10691":1,"10692":1,"10693":1,"10694":1,"10695":1,"10696":1,"10697":1,"10698":0,"10699":0,"10700":1,"10701":1,"10702":0,"10703":0,"10704":1,"10705":1,"10706":0,"10707":0,"10708":1,"10709":1,"10710":0,"10711":0,"10712":1,"10713":1,"10714":1,"10715":1,"10716":1,"10717":1,"10718":1,"10719":1,"10720":1,"10721":1,"10722":1,"10723":1,"10724":1,"10725":1,"10726":1,"10727":1,"10728":1,"10729":1,"10730":1,"10731":1,"10732":1,"10733":1,"10734":1,"10735":1,"10736":1,"10737":1,"10738":1,"10739":1,"10740":1,"10741":1,"10742":1,"10743":1,"10744":1,"10745":1,"10746":1,"10747":1,"10748":1,"10749":1,"10750":1,"10751":0,"10752":0,"10755":0,"10756":0,"10757":0,"10758":0,"10759":1,"10760":1,"10761":1,"10762":1,"10763":1,"10764":1,"10765":1,"10766":1,"10767":1,"10768":1,"10769":1,"10770":1,"10771":1,"10772":1,"10773":1,"10774":1,"10775":1,"10776":1,"10777":0,"10778":0,"10779":1,"10780":1,"10781":0,"10782":1,"10783":1,"10784":0,"10785":1,"10786":1,"10787":1,"10788":1,"10789":1,"10790":1,"10791":1,"10792":1,"10793":1,"10794":1,"10795":1,"10796":1,"10797":1,"10798":1,"10799":1,"10800":1,"10801":1,"10802":1,"10803":1,"10804":1,"10805":1,"10806":1,"10807":1,"10808":1,"10809":1,"10810":1,"10811":1,"10812":1,"10813":1,"10814":1,"10815":1,"10816":1,"10817":0,"10818":0,"10819":0,"10820":0,"10821":0,"10822":0,"10823":1,"10824":1,"10825":1,"10826":1,"10827":0,"10828":1,"10829":1,"10830":0,"10831":0,"10832":0,"10833":0,"10834":1,"10835":1,"10836":1,"10837":1,"10838":1,"10839":0,"10840":0,"10841":0,"10842":0,"10844":0,"10845":1,"10846":1,"10847":1,"10848":1,"10849":0,"10850":0,"10851":0,"10852":0,"10853":0,"10855":0,"10859":1,"10860":0,"10861":0,"10862":1,"10863":1,"10864":1,"10865":1,"10866":1,"10867":0,"10868":0,"10869":0,"10870":1,"10871":1,"10872":1,"10873":0,"10874":1,"10875":0,"10876":1,"10877":0,"10878":0,"10879":1,"10880":1,"10881":1,"10882":0,"10883":0,"10884":0,"10885":0,"10889":1,"10890":1,"10891":1,"10892":1,"10893":1,"10894":1,"10895":1,"10896":1,"10897":1,"10898":1,"10899":1,"10900":1,"10901":1,"10902":1,"10903":1,"10904":1,"10905":1,"10906":1,"10907":1,"10908":1,"10909":1,"10910":1,"10911":1,"10912":1,"10913":1,"10914":1,"10915":1,"10916":1,"10917":1,"10918":1,"10919":1,"10920":1,"10921":1,"10922":1,"10923":1,"10924":1,"10925":0,"10926":0,"10927":0,"10928":0,"10929":1,"10930":1,"10931":1,"10932":0,"10933":0,"10934":0,"10935":1,"10936":1,"10937":1,"10938":0,"10939":0,"10940":1,"10941":1,"10942":1,"10943":1,"10944":0,"10945":1,"10946":0,"10947":0,"10948":1,"10949":0,"10950":1,"10951":0,"10952":1,"10953":1,"10954":0,"10955":0,"10956":0,"10957":0,"10958":0,"10959":0,"10960":0,"10961":0,"10962":0,"10963":0,"10964":0,"10965":0,"10966":0,"10967":0,"10968":0,"10969":1,"10970":1,"10971":1,"10972":0,"10973":0,"10974":1,"10975":0,"10976":1,"10977":1,"10978":0,"10979":0,"10980":0,"10981":0,"10982":1,"10983":1,"10984":0,"10985":0,"10986":0,"10987":0,"10988":1,"10989":1,"10990":0,"10991":0,"10992":1,"10993":0,"10994":1,"10995":0,"10996":0,"10997":1,"10998":0,"10999":0,"11000":0,"11001":1,"11002":0,"11003":0,"11004":1
// }

// for (const key in tarHandIdxDic) {
//     if (tarHandIdxDic.hasOwnProperty(key)) {
//         const val = tarHandIdxDic[key];
//         if (val === 0) {
//             let handDicKey = keyArr[key];
//             console.log(handDicKey)
//             let fileName = handDicKey.split('-')[0];
//             let idx = parseInt(handDicKey.split('-')[1]);
//             let _dic1 = JSON.parse(fs.readFileSync(`${resultPath}/${fileName}`).toString());
//             _dic1.match[idx].isNeedDelete = key.toString();
//             fs.writeFileSync(`${resultPath}/${fileName}`, JSON.stringify(_dic1));
//         }
//     }
// }

// for (let i = 0; i < files.length; i++) {
//     const fileName = files[i];
//     const json = JSON.parse(fs.readFileSync(`${resultPath}/${fileName}`).toString());
//     let tarMatch = [];
//     for (let j = 0; j < json.match.length; j++) {
//         const _match = json.match[j];
//         if (!_match.isNeedDelete) {
//             tarMatch.push(_match);
//         } else {
//             console.log(fileName, _match)
//         }
//     }
//     json.match = tarMatch;
//     fs.writeFileSync(`${resultOutPath}/${fileName}`, JSON.stringify(json));
// }